package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CompanyCerifyBean;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 15/12/17.
 */
public class CompanyCertiFragment extends Fragment {
    
    UserPresenter presenter;
    LoadingDialog dialog;
    String id;
    
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.date)
    TextView date;
    @Bind(R.id.product)
    TextView product;
    @Bind(R.id.legal_representative)
    TextView legalRepresentative;
    @Bind(R.id.company_representative)
    TextView companyRepresentative;
    @Bind(R.id.operating_period)
    TextView operatingPeriod;
    @Bind(R.id.registration_authority)
    TextView registrationAuthority;

    // TODO: 16/3/28
    public CompanyCertiFragment (String id) {
        this.id = id;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_company_certi, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        dialog = new LoadingDialog(getActivity());
        presenter = new UserPresenterImp();

        presenter.getCompanyCerify(id);
        dialog.show();
    }

    public void onEventMainThread(CompanyCerifyBean bean) {
        dialog.hide();
        if (bean.getReturn_code() == 0) {
            name.setText(bean.getData().getIntro_name());
            address.setText(bean.getData().getCertify_place());
            date.setText(bean.getData().getCertify_date());
            product.setText(bean.getData().getIntro_mainpro());
            legalRepresentative.setText(bean.getData().getCertify_corporation());
            companyRepresentative.setText(bean.getData().getCertify_represent());
            operatingPeriod.setText(bean.getData().getCertify_end());
            registrationAuthority.setText(bean.getData().getCertify_gear());
        } else {
            DeviceUtil.MyToast(getActivity(), bean.getMsg());
        }
    }

    public void onEventMainThread(ErrorBean bean) {
       DeviceUtil.MyToast(getActivity(), bean.getErrorMsg());
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
